package com.mocai.merchant.core.service;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mocai.merchant.core.domain.Pageable;
import com.mocai.merchant.core.mapper.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 业务基础类
 *
 * @author zhangguangyong <a href="#">1243610991@qq.com</a>
 * @date 2017/1/7 23:04
 * @sine 0.0.1
 */
public class BaseService<M extends BaseMapper<E, ID>, E, ID extends Serializable> {
    @Autowired
    M mapper;

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    public int delete(ID id) {
        return mapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     * @return
     */
    public int delete(List<ID> ids) {
        return mapper.batchDelete(ids);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    public int save(E entity) {
        return mapper.insert(entity);
    }

    /**
     * 批量保存
     *
     * @param entities
     * @return
     */
    public int save(List<E> entities) {
        return mapper.batchInsert(entities);
    }

    /**
     * 保存不为Null的字段
     *
     * @param entity
     * @return
     */
    public int saveSelective(E entity) {
        return mapper.insertSelective(entity);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public E find(ID id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<E> find(List<ID> ids) {
        return mapper.selectByPrimaryKeys(ids);
    }

    /**
     * 以实体作为条件查询
     *
     * @param entity
     * @return
     */
    public List<E> find(E entity) {
        return mapper.selectByEntity(entity);
    }

    /**
     * 自定义key value 条件查询
     *
     * @param conditions
     * @return
     * @see BaseMapper#selectByConditions(Map)
     */
    public List<E> find(Map conditions) {
        return mapper.selectByConditions(conditions);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    public Pageable findPage(Pageable page) {
        PageInfo pageResult = PageHelper.offsetPage(page.getOffset(), page.getPageSize()).doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                mapper.selectAll();
            }
        });
        page.setTotalElements(pageResult.getTotal());
        page.setContent(pageResult.getList());
        return page;
    }

    /**
     * 以实体作为条件的分页查询
     *
     * @param page
     * @param entity
     * @return
     */
    public Pageable find(Pageable page, E entity) {
        PageInfo pageResult = PageHelper.offsetPage(page.getOffset(), page.getPageSize()).doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                mapper.selectByEntity(entity);
            }
        });
        page.setTotalElements(pageResult.getTotal());
        page.setContent(pageResult.getList());
        return page;
    }

    /**
     * 以自定义参数作为条件的实体查询
     *
     * @param page
     * @param conditions
     * @return
     */
    public Pageable find(Pageable page, Map conditions) {
        PageInfo pageResult = PageHelper.offsetPage(page.getOffset(), page.getPageSize()).doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                mapper.selectByConditions(conditions);
            }
        });
        page.setTotalElements(pageResult.getTotal());
        page.setContent(pageResult.getList());
        return page;
    }

    /**
     * 更新不为Null的字段
     *
     * @param entity
     * @return
     */
    public int updateSelective(E entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 更新
     *
     * @param entity
     * @return
     */
    public int update(E entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    /**
     * 批量更新
     *
     * @param entities
     * @return
     */
    public int update(List<E> entities) {
        return mapper.batchUpdate(entities);
    }


}
